package com.carleasoft.mps.adu.business.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.adu.business.user.bean.entity.AuthorityUserEntity;
import com.carleasoft.mps.adu.business.user.bean.po.AuthorityAdminPo;
import com.carleasoft.mps.adu.business.user.bean.po.AuthorityUserPo;
import com.carleasoft.mps.adu.business.user.bean.po.UserQueryPO;
import com.carleasoft.mps.adu.business.user.bean.vo.DeptUserTreeVo;
import com.carleasoft.mps.adu.business.user.service.AuthorityUserAdminService;
import com.carleasoft.mps.adu.business.user.service.AuthorityUserDeptService;
import com.carleasoft.mps.adu.business.user.service.AuthorityUserService;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.user.SortPo;
import com.carleasoft.mps.core.user.ThreadUserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;


/**
 * <p>Description: User controller</p>
 *
 * @author ztm
 * @since 2020-04-29
 */
@RestController
@RequestMapping("/user")
public class AuthorityUserController{

    @Autowired
    AuthorityUserService authorityUserService;
    @Autowired
    AuthorityUserAdminService authorityUserAdminService;
    @Autowired
    AuthorityUserDeptService authorityUserDeptService;
    /**
     * 添加用户
     * 修改ok
     * @param authorityUserPo
     * @return ResultBody
     */
    @PostMapping("/add")
    public R addUser(@RequestBody AuthorityUserPo authorityUserPo) {
        return R.ok(authorityUserService.addUser(authorityUserPo));
    }

    /**
     * 修改用户
     * ok
     *
     * @param authorityUserPo
     * @return ResultBody
     */
    @PostMapping("/update")
    public R updateUser(@RequestBody AuthorityUserPo authorityUserPo) {
        return R.ok(authorityUserService.updateUser(authorityUserPo));
    }

    /**
     * 删除用户
     * R
     *
     * @param authorityUserPo
     * @return ResultBody
     */
    @PostMapping("/delete")
    public R deleteUser(@RequestBody AuthorityUserPo authorityUserPo) {
        return R.result(authorityUserService.deleteUser(authorityUserPo));
    }

    /**
     * 获取信息列表,分页
     * ok
     *
     * @return ResultBody
     */
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody UserQueryPO userQueryPO) {
        return R.ok(authorityUserDeptService.pageQuery(new Page(userQueryPO.getCurrent(),userQueryPO.getSize()),userQueryPO));
    }

    @PostMapping("/getByDept")
    public R getByDept(@RequestBody UserQueryPO authorityUserPo){
        return R.ok(authorityUserDeptService.getByDept(authorityUserPo));
    }


    /**
     * 用户详情查询
     * ok
     * @param authorityUserPo
     * @return ResultBody
     */
    @PostMapping("/getUserByCode")
    public R getUserByCode(@RequestBody AuthorityUserPo authorityUserPo) {
        return R.ok(authorityUserService.getUserByCode(authorityUserPo));
    }

    @PostMapping("/getByUserAccount")
    public R getByUserAccount(@RequestBody AuthorityUserPo authorityUserPo){
        return R.ok(authorityUserService.getByUserAccount(authorityUserPo));
    }

    /**
     * 重置密码
     * ok
     *
     * @param authorityUserPo
     * @return ResultBody
     */
    @PostMapping("/changePassword")
    public R changePassword(@RequestBody AuthorityUserPo authorityUserPo) {
        return R.ok(authorityUserService.changePassword(authorityUserPo));
    }

    @PostMapping("/updateSort")
    public R updateSort(@RequestBody SortPo sortPo){
        return R.result(authorityUserDeptService.updateSort(sortPo));
    }


    /**
     * 给每个组织添加唯一组织管理员
     * @return
     */
    @PostMapping("/addOrgAdmin")
    public R addOrgAdmin(@RequestBody AuthorityAdminPo authorityAdminPo){
        return R.ok(authorityUserAdminService.addOrgAdmin(authorityAdminPo));
    }

    /**
     * 添加二级管理员，包括系统二级或者组织的二级管理员
     * @param authorityAdminPo
     * @return
     */
    @PostMapping("/addAdministrator")
    public R addSystemAdmin(@RequestBody AuthorityAdminPo authorityAdminPo){
        authorityAdminPo.setDeptCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
        return R.ok(authorityUserAdminService.addSystemAdmin(authorityAdminPo));
    }

    /**
     *二级管理员列表查询
     * @return
     */
    @PostMapping("/administratorsList")
    public R administratorsList(@RequestBody AuthorityAdminPo authorityAdminPo){
        return R.ok(authorityUserAdminService.administratorsList(authorityAdminPo));
    }

    /**
     * @author zang
     * @desc 获取部门用户选择树
     * @date 2023-05-24
     */
    @PostMapping("/getDeptUserTree")
    public R getDeptUserTree(){
        List<DeptUserTreeVo> res =  authorityUserAdminService.getDeptUserTree();
        return R.ok(res);
    }

}
